//开发工具初始化
try
{
	if (window.self.frameElement == null) 
	{			
		if (window.addEventListener) {
			window.addEventListener("load", dev_settings, false);
		} else {
			window.attachEvent("onload", dev_settings);
		}
	}

}
catch (err)
{
	console.log(err.name + ": " + err.message);
}

var $devRender = null;
function dev_settings () 
{
	window.setTimeout( function () {
		if ($devRender == null || $devRender.flag == false)
		{
			$devRender = new Dev_Render();
			$devRender.settings();
		}
	}, 1);
}

function Dev_Render()
{
	this.initialize.apply(this, arguments)
}

Dev_Render.prototype = {
	initialize : function ()
	{
	},
	settings : function ()
	{
		if (!this.flag)
		{
			this.setDivDrag();
			this.flag = true;
		}
	},
	setDivDrag : function ()
	{
		var toolsDiv = document.getElementById("$DevToolsDiv");
		if (!toolsDiv) {
			toolsDiv = document.createElement("div");
			toolsDiv.id = "$DevToolsDiv";
			toolsDiv.style.cssText="line-height: 18px;z-index:99999;";
			
			var handle = document.createElement("div");
			handle.id = "$DragHandle";
			handle.innerText="开发者工具"
			handle.style.cssText="padding: 5px;background:#7EACF5;";
			
			var toolList = document.createElement("div");
			toolList.id = "$ToolList";
			$(toolList).html(
					"<li><a title='修改*Dao.xml时，可以不用重启重新装载' href='services/manage/orm/reload/do'>reload-DAO</a></li>" +
					"<li><a title='修改config/*_i18n*.properties或通过数据库方式修改PLT_I18N_T时，可以不用重启重新装载' href='services/manage/i18n/reload'>reload-I18N</a></li>" +
					"<li><a title='通过数据库方式修改PLT_ENUM_T时，可以不用重启重新装载' href='services/manage/enums/reload'>reload-ENUM</a></li>" +
					"<li><a title='修改*.registry.xml或通过数据库方式修改PLT_REGISTRY_T时，可以不用重启重新装载' href='services/manage/registry/reload'>reload-REG</a></li>"/* +
					"<li><a title='风格切换' onclick='switchStyle();void(0);'>SwitchStyle</a></li>" +
					"<li><a title='清空EhCache缓存' href='services/manage/orm/reload/do'>clean-CACHE</a></li>"*/
					)
			
			toolsDiv.appendChild(handle);
			toolsDiv.appendChild(toolList);
			document.body.appendChild(toolsDiv);
			
		}
		this.nDrag = new Dev_Drag(toolsDiv, {handle:toolsDiv, limit:false});
	},
	removeSettings : function ()
	{
		try
		{
			if (this.flag)
			{
				for (var i = 0; i < this.borderDivs.length; i++) 
				{
					var div = document.getElementById(this.borderDivs[i].id);
					if (document.getElementById(this.borderDivs[i].id))
					{
						document.body.removeChild(this.borderDivs[i]);
					}
				}

				var headNode = document.getElementsByTagName('head')[0];			
				this.link.href = this.originalLink != null ? this.originalLink.href : "";
				
				window.clearInterval(this.timer);
				if (document.title.slice(0,this.prefix.length) == this.prefix)
				{
					document.title = document.title.substring(this.prefix.length,document.title.length);
				}
				
				this.nDrag.stopDrag();
				var img = document.getElementById(this.nDrag.drag.id);
				if (img)
				{
					document.body.removeChild(this.nDrag.drag);
				}
				this.flag = false;
			}
		}
		catch (err)
		{
			console.log(err.name + ": " + err.message);
		}
		
	}
};

// 浮动层可拖动组件
function Dev_Drag()
{
	this.initialize.apply(this, arguments)
}

Dev_Drag.prototype = {
	initialize : function (drag, options)
	{
		this.drag = this.$(drag);		

		this._x = this._y = 0;
		this._moveDrag = this.bind(this, this.moveDrag);
		this._stopDrag = this.bind(this, this.stopDrag);
		
		this.setOptions(options);
		
		this.handle = this.$(this.options.handle);
		this.maxContainer = this.$(this.options.maxContainer);
		
		this.maxTop = Math.max(this.maxContainer.clientHeight, this.maxContainer.scrollHeight) - this.drag.offsetHeight;
		this.maxLeft = Math.max(this.maxContainer.clientWidth, this.maxContainer.scrollWidth) - this.drag.offsetWidth;
		
		this.limit = this.options.limit;
		this.lockX = this.options.lockX;
		this.lockY = this.options.lockY;
		this.lock = this.options.lock;
		
		this.onStart = this.options.onStart;
		this.onMove = this.options.onMove;
		this.onStop = this.options.onStop;
		
		this.handle.style.cursor = "move";
		
		this.changeLayout();
		
		this.addHandler(this.handle, "mousedown", this.bind(this, this.startDrag));
		this.addHandler(window, "scroll", this.bind(this, this.scroll));
		this.addHandler(window, "resize", this.bind(this, this.changeLayout));
		
	},
	changeLayout : function ()
	{
		var htmlObj = document.getElementsByTagName('html')[0];

		this.drag.style.top = "150px";
		this.drag.style.left = (htmlObj.clientWidth - 80) + "px";
		this.drag.style.position = "absolute";
		this.drag.style.margin = "0"
	},
	startDrag : function (event)
	{		
		var event = event || window.event;
		
		this._x = event.clientX - this.drag.offsetLeft;
		this._y = event.clientY - this.drag.offsetTop;
		
		this.addHandler(document, "mousemove", this._moveDrag);
		this.addHandler(document, "mouseup", this._stopDrag);
		
		event.preventDefault && event.preventDefault();
		this.handle.setCapture && this.handle.setCapture();
		
		this.onStart()
	},
	moveDrag : function (event)
	{
		var event = event || window.event;
		
		var iTop = event.clientY - this._y;
		var iLeft = event.clientX - this._x;		
		
		if (this.lock) return;
		
		this.limit && (iTop < 0 && (iTop = 0), iLeft < 0 && (iLeft = 0), iTop > this.maxTop && (iTop = this.maxTop), iLeft > this.maxLeft && (iLeft = this.maxLeft));
		
		this.lockY || (this.drag.style.top = iTop + "px");
		this.lockX || (this.drag.style.left = iLeft + "px");

		event.preventDefault && event.preventDefault();
		
		this.onMove()
	},
	stopDrag : function ()
	{
		this.removeHandler(document, "mousemove", this._moveDrag);
		this.removeHandler(document, "mouseup", this._stopDrag);
		
		this.handle.releaseCapture && this.handle.releaseCapture();
		
		this.onStop()
	},
	scroll : function ()
	{
		var top = this.getTop(this.drag);
		var isIE6 = /msie 6/i.test(navigator.userAgent);
		var bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
		this.drag.style.position = (isIE6) ? "absolute" : "fixed";
		this.drag.style.top = (isIE6) ? bodyScrollTop + "px" :  this.drag.style.top;
	},
	getTop : function (e)
	{
		var offset = e.offsetTop;
		if(e.offsetParent != null) offset += this.getTop(e.offsetParent);
		return offset;
	},
	setOptions : function (options)
	{
		this.options =
		{
			handle:			this.drag,
			limit:			true,
			lock:			false,
			lockX:			false,
			lockY:			false,
			maxContainer:	document.documentElement || document.body, // 指定限制容器
			onStart:		function () {},
			onMove:			function () {},
			onStop:			function () {}
		};
		for (var p in options) this.options[p] = options[p]
	},
	// 获取id
	$ : function (id)
	{
		return typeof id === "string" ? document.getElementById(id) : id
	},
	addHandler : function (oElement, sEventType, fnHandler)
	{
		return oElement.addEventListener ? oElement.addEventListener(sEventType, fnHandler, false) : oElement.attachEvent("on" + sEventType, fnHandler)
	},
	removeHandler : function (oElement, sEventType, fnHandler)
	{
		return oElement.removeEventListener ? oElement.removeEventListener(sEventType, fnHandler, false) : oElement.detachEvent("on" + sEventType, fnHandler)
	},
	bind : function (object, fnHandler)
	{
		return function ()
		{
			return fnHandler.apply(object, arguments)	
		}
	}
};

function switchStyle(){
	if($.cookie('THEME') == 'ccay'){
		$.cookie('THEME', 'default'); 
		window.location.href = window.location.href.replace("#!ccay","#!ccay");
		
	}else{
		$.cookie('THEME', 'ccay'); 
		window.location.href = window.location.href.replace("#!ccay","#!ccay");
	}
}